<template>

  <div style="margin:40px;">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="margin:0;font-size:18px;font-weight:bold;">{{ this.$route.meta.title }}</span>
        <el-button style="float: right; padding:3px 0" type="text" @click="cancelForm">返回列表</el-button>
      </div>
      <div style="max-width:1000px;margin:auto;">
        <el-form
          ref="form"
          :model="form"
          :rules="form.houseType==0?rulesPesonal:rules"
          class="table-form"

          size="small"
        >
          <table class="table table-bordered th-bg">
            <tbody>
            <tr>
              <th width="200">项目名称</th>
              <td >
                <el-form-item prop="projectName">
                  <el-input v-model="form.projectName"/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th width="200">政策分类</th>
              <td>
                <el-form-item prop="category">
                  <el-select v-model="form.category" clearable size="small" placeholder="请选择政策分类" style="width: 200px;" class="filter-item" >
                    <el-option
                      v-for="item in policyCategoryList"
                      :key="item.label"
                      :label="item.label"
                      :value="item.label"
                    />  
                  </el-select>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th width="200">申报条件</th>
              <td>
                <el-form-item prop="requirements" class="editor">
                  <div style="border: 1px solid #ccc;">
                    <Toolbar
                      style="border-bottom: 1px solid #ccc"
                      :editor="editor"
                      :defaultConfig="toolbarConfig"
                      :mode="mode"
                    />
                    <Editor
                      style="height: 500px; overflow-y: hidden; word-wrap: break-word;"
                      v-model="form.requirements"
                      :defaultContent="defaultContent"
                      :defaultConfig="editorConfig"
                      :mode="mode"
                      @onCreated="onCreated"
                    />
                  </div>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th width="200">咨询电话</th>
              <td >
                <el-form-item prop="contact">
                  <el-input v-model="form.contact"/>
                </el-form-item>
                <div>多个联系电话使用';'分隔</div>
              </td>
            </tr>
            <tr>
              <th width="200">备注</th>
              <td >
                <el-form-item prop="note">
                  <el-input v-model="form.note"/>
                </el-form-item>
              </td>
            </tr>
            </tbody>
          </table>
          <div style="display:flex; align-item: center; justify-content: center; margin-top:20px;">
            <el-button
              :loading="loading"
              type="primary"
              size="small"
              @click="submitForm"
            >
              {{isEdit ? '更新政策' : '新增政策'}} 
            </el-button>            
          </div>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import crudPolicy from '@/api/business/policy'
import { MessageBox } from 'element-ui'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { policyCategoryList } from '@/utils/index'
import { form } from '@/components/Crud/crud'

const defaultForm = {
  
}

export default {
  components: {
    Editor, 
    Toolbar
  },
  computed: {
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      policyCategoryList,
      form: JSON.parse(JSON.stringify(defaultForm)),
      rules: {
        contact: [
          {
            required: true,
            message: '请输入咨询电话'
          }
        ],
        projectName: [
          {
            required: true,
            message: '请输入项目名称'
          }
        ],
        category: [
          {
            required: true,
            message: '请选择政策分类'
          }
        ]
      },
      loading: false,
      id: null,
      editor: null,
      html: '',
      toolbarConfig: { 
        excludeKeys: [
	        'fullScreen',
          'uploadVideo',
          'uploadImage'
	      ]
      },
      editorConfig: { 
        placeholder: '请输入内容...',
        MENU_CONF: {
          lineHeight: {
            lineHeightList: ['0.5', '1', '1.5', '2', '2.5']
          }
        }
      },
      mode: 'default', // or 'simple'
      defaultContent : [
        {
          type: 'paragraph',
          lineHeight: '1.5',
          children: [
            { text: '', fontSize: '15px' },
          ]
        }
      ]
    }
  },

  created() {
    if (this.isEdit) {
      this.id = this.$route.params && this.$route.params.id
      this.fetchData(this.id)
    }else {
    }
  },
  computed: {
    
  },
  methods: {
    fetchData(id) {
      crudPolicy.get(id).then(response => {
        this.form = Object.assign({}, this.form, response.data)       
      }).catch(err => {
        console.log(err)
      })
    },
    cancelForm() {
      this.$router.push('/business/policy')
    },

    submitForm() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return
        }
        MessageBox.confirm('您确认执行此操作吗？', '确认操作', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          if (this.isEdit) {
            crudPolicy.edit(this.form).then(() => {
              this.$notify({
                title: '成功',
                message: '修改信息成功',
                type: 'success',
                duration: 2000
              })
              this.loading = false
              this.cancelForm()
            }).catch(err => {
              console.log(err)
              this.loading = false
            })
          } else {
            crudPolicy.add(this.form).then(() => {
              this.$notify({
                title: '成功',
                message: '提交信息成功',
                type: 'success',
                duration: 2000
              })
              this.loading = false
              this.cancelForm()
            }).catch(err => {
              console.log(err)
              this.loading = false
            })
          }
        }).catch((e) => {
          console.log(e)
        });
      })
    },
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
  },
  mounted() {
    // 模拟 ajax 请求，异步渲染编辑器
    setTimeout(() => {
      this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
    }, 1500)
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  }
}
</script>



<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}

.amap-wrapper {
  width: 500px;
  height: 500px;
}

::v-deep .map-dialog .el-dialog__body {
  padding: 10px 20px;
}

</style>
<style src="@wangeditor/editor/dist/css/style.css"></style>